package Controller;

import Entity.Experiment;
import Entity.MyExp;
import Entity.Selection;
import Service.ExperimentService;
import Service.SelectionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class SelectServlet extends HttpServlet { //��ѯ�Ѿ�ѡ���ʵ��
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	//new add
    	String op=request.getParameter("op");
    	if("upload".equals(op)){
    		HttpSession session = request.getSession(false);
    		String uid=(String)session.getAttribute("uid");
    		String uname=(String)session.getAttribute("uname");
    		String eid=request.getParameter("eid");
    		String sid=request.getParameter("sid");
    		request.setAttribute("eid", eid);
    		request.setAttribute("sid", sid);
    		request.setAttribute("uid", uid);
    		request.setAttribute("uname",uname);
    		request.getRequestDispatcher("/WEB-INF/page/Upload.jsp").forward(request, response);
    		return;
    	}
    	
    	
    	
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession(false);
        if (session == null) {
            request.getRequestDispatcher("/WEB-INF/page/login.jsp").forward(request, response);
        }
        
        SelectionService ss = new SelectionService();
        ExperimentService es = new ExperimentService();
        String unum = (String)session.getAttribute("uid");
        List<Selection> slist = ss.findByStu_number(unum);   // ��ѯ����ѡ���
        List<MyExp> mlist = new ArrayList<MyExp>(); 
        for(Selection s:slist){
            Experiment e = es.findExperimentById(s.getEx_id());
            mlist.add(new MyExp(s,e));
        }
        request.setAttribute("elist", mlist);
        request.getRequestDispatcher("/WEB-INF/page/ChoosedExp.jsp").forward(request, response);
    }
}
